//
//  ModelProductPackageList.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/15.
//

#import "ModelProductPackageList.h"
#import "ModelPairs.h"

NSString *const kProductPackageListAmount = KEY_amount;
NSString *const kProductPackageListSlogan = KEY_slogan;
NSString *const kProductPackageListIsReloan = KEY_isReloan;
NSString *const kProductPackageListId = KEY_id;
NSString *const kProductPackageListTermUnit = KEY_termUnit;
NSString *const kProductPackageListTag = KEY_tag;
NSString *const kProductPackageListName = KEY_name;
NSString *const kProductPackageListTerm = KEY_term;
NSString *const kProductPackageListRemark = KEY_remark;
NSString *const kProductPackageListPairs = KEY_pairs;

@interface ModelProductPackageList ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ModelProductPackageList

@synthesize amount = _amount;
@synthesize slogan = _slogan;
@synthesize isReloan = _isReloan;
@synthesize productPackageListIdentifier = _productPackageListIdentifier;
@synthesize termUnit = _termUnit;
@synthesize tag = _tag;
@synthesize name = _name;
@synthesize term = _term;
@synthesize remark = _remark;
@synthesize pairs = _pairs;

+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
        self.amount = [[self objectOrNilForKey:kProductPackageListAmount fromDictionary:dict] doubleValue];
        self.slogan = [self objectOrNilForKey:kProductPackageListSlogan fromDictionary:dict];
        self.isReloan = [[self objectOrNilForKey:kProductPackageListIsReloan fromDictionary:dict] doubleValue];
        self.productPackageListIdentifier = [[self objectOrNilForKey:kProductPackageListId fromDictionary:dict] doubleValue];
        self.termUnit = [[self objectOrNilForKey:kProductPackageListTermUnit fromDictionary:dict] doubleValue];
        self.tag = [self objectOrNilForKey:kProductPackageListTag fromDictionary:dict];
        self.name = [self objectOrNilForKey:kProductPackageListName fromDictionary:dict];
        self.term = [[self objectOrNilForKey:kProductPackageListTerm fromDictionary:dict] doubleValue];
        self.remark = [self objectOrNilForKey:kProductPackageListRemark fromDictionary:dict];
        NSObject *receivedPairsList = [dict objectForKey:kProductPackageListPairs];
        NSMutableArray *parsedPairsList = [NSMutableArray array];
        if ([receivedPairsList isKindOfClass:[NSArray class]]) {
            for (NSDictionary *item in (NSArray *)receivedPairsList) {
                if ([item isKindOfClass:[NSDictionary class]]) {
                    [parsedPairsList addObject:[ModelPairs modelObjectWithDictionary:item]];
                }
            }
        } else if ([receivedPairsList isKindOfClass:[NSDictionary class]]) {
            [parsedPairsList addObject:[ModelPairs modelObjectWithDictionary:(NSDictionary *)receivedPairsList]];
        }
        self.pairs = [NSArray arrayWithArray:parsedPairsList];
    }
    return self;
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.amount] forKey:kProductPackageListAmount];
    [mutableDict setValue:self.slogan forKey:kProductPackageListSlogan];
    [mutableDict setValue:[NSNumber numberWithDouble:self.isReloan] forKey:kProductPackageListIsReloan];
    [mutableDict setValue:[NSNumber numberWithDouble:self.productPackageListIdentifier] forKey:kProductPackageListId];
    [mutableDict setValue:[NSNumber numberWithDouble:self.termUnit] forKey:kProductPackageListTermUnit];
    [mutableDict setValue:self.tag forKey:kProductPackageListTag];
    [mutableDict setValue:self.name forKey:kProductPackageListName];
    [mutableDict setValue:[NSNumber numberWithDouble:self.term] forKey:kProductPackageListTerm];
    [mutableDict setValue:self.remark forKey:kProductPackageListRemark];
    NSMutableArray *tempArrayForProductPackageList = [NSMutableArray array];
    for (NSObject *subArrayObject in self.pairs) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            [tempArrayForProductPackageList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            [tempArrayForProductPackageList addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForProductPackageList] forKey:kProductPackageListPairs];
    
    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}

@end
